package com.ktjy.utils;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.amqp.core.Queue;

@Configuration
public class RabbitMQConfig {
    /**
     * 定义简单队列
     *
     * @return
     */
    @Bean
    public Queue simpleQueue() {
        System.out.println("simpleQueue init");
        return new Queue("spring_simple_queue");
    }

    /**
     * 工作队列模式(Work Queues)
     */
    @Bean
    public Queue workQueue() {
        return new Queue("spring_work_queue");
    }

    // ---------------发布订阅模式（Pub/Sub）开始---------------
    @Bean
    public Queue sendSmsQueue() {
        return new Queue("sms_queue");
    }

    @Bean
    public Queue sendEmailQueue() {
        return new Queue("email_queue");
    }

    @Bean // 声明交换机
    public FanoutExchange exchangeFanout() {
        // 创建一个扇出交换机
        return new FanoutExchange("exchange_fanout");
    }

    @Bean // 声明绑定信息
    public Binding bindingSms() {
        // 扇出型不需要设置 RoutingKey
        return BindingBuilder.bind(sendSmsQueue()).to(exchangeFanout());
    }

    @Bean
    public Binding bindingEmail() {
        // 扇出型不需要设置 RoutingKey
        return BindingBuilder.bind(sendEmailQueue()).to(exchangeFanout());
    }
    // ---------------发布订阅模式（Pub/Sub）结束---------------
}
